package com.huhu.myharmony;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.CommonDialog;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 * The MainAbilitySlice, the first page of app
 */
public abstract class BaseAbilitySlice extends AbilitySlice {
    protected static final HiLogLabel loglabel = new HiLogLabel(HiLog.LOG_APP, 0x11102, "BaseAbilitySlice");

    private CommonDialog commonDialog;
    private int roateNum = 0;


    /**
     * The showProgress, when loginButton clicked, the dialog show progress
     *
     * @param show show, show or hide the dialog
     */
    public void showProgress(final boolean show) {
        // Instance the dialog when dialog is null
        if (commonDialog == null) {
            commonDialog = new CommonDialog(this);

            // Get circleProgress animation
            Component circleProgress = drawCircleProgress(AttrHelper.vp2px(6, this), AttrHelper.vp2px(3, this));
            commonDialog
                    .setContentCustomComponent(circleProgress)
                    .setTransparent(true)
                    .setSize(
                            DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        }

        // Show or hide the dialog
        if (show) {
            commonDialog.show();
        } else {
            commonDialog.destroy();
            commonDialog = null;
        }
    }

    /**
     * The drawCircleProgress, draw circle progress function
     *
     * @param maxRadius maxRadius,the radius of animation for the max circle
     * @param minRadius minRadius,the radius of animation for the min circle
     * @return The component of the animation
     */
    private Component drawCircleProgress(int maxRadius, int minRadius) {
        final int circleNum = 12;

        // Init the paint
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL_STYLE);
        paint.setColor(Color.WHITE);

        // Init the component
        Component circleProgress = new Component(this);
        circleProgress.setComponentSize(AttrHelper.vp2px(100, this), AttrHelper.vp2px(100, this));
        circleProgress.setBackground(new ShapeElement(this, ResourceTable.Graphic_login_dialog_bg));

        // Draw the animation
        circleProgress.addDrawTask(
                (component, canvas) -> {
                    // Reset when a round
                    if (roateNum == circleNum) {
                        roateNum = 0;
                    }

                    // Rotate the canvas
                    canvas.rotate(30 * roateNum, (float) (component.getWidth() / 2), (float) (component.getHeight() / 2));
                    roateNum++;
                    int radius = (Math.min(component.getWidth(), component.getHeight())) / 2 - maxRadius;
                    float radiusIncrement = (float) (maxRadius - minRadius) / circleNum;
                    double angle = 2 * Math.PI / circleNum;

                    // Draw the small circle
                    for (int i = 0; i < circleNum; i++) {
                        float x = (float) (component.getWidth() / 2 + Math.cos(i * angle) * radius);
                        float y = (float) (component.getHeight() / 2 - Math.sin(i * angle) * radius);
                        paint.setAlpha((1 - (float) i / circleNum));
                        canvas.drawCircle(x, y, maxRadius - radiusIncrement * i, paint);
                    }

                    // Refresh the component delay
                    getUITaskDispatcher()
                            .delayDispatch(
                                    circleProgress::invalidate,
                                    150);
                });
        return circleProgress;
    }

    /**
     * The showLoginDialog, show the result of login whether success or fail, can clicked outside to cancel the dialog
     *
     * @param success success, success or fail
     */
    private void showLoginDialog(boolean success) {
        // Init dialog
        CommonDialog loginDialog = new CommonDialog(this);
        // Get component from xml
        Component layoutComponent =
                LayoutScatter.getInstance(this).parse(ResourceTable.Layout_login_dialog, null, false);
        Text dialogText = (Text) layoutComponent.findComponentById(ResourceTable.Id_dialog_text);
        Text dialogSubText = (Text) layoutComponent.findComponentById(ResourceTable.Id_dialog_sub_text);

        if (success) {
            dialogText.setText(ResourceTable.String_success);
            dialogSubText.setText(ResourceTable.String_loginSuccess);
        } else {
            dialogText.setText(ResourceTable.String_fail);
            dialogSubText.setText(ResourceTable.String_loginFail);
        }

        loginDialog
                .setContentCustomComponent(layoutComponent)
                .setTransparent(true)
                .setSize(AttrHelper.vp2px(300, this), DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                .setAutoClosable(true);

        loginDialog.show();
    }

    @Override
    protected void onResult(int requestCode, Intent resultIntent) {
        super.onResult(requestCode, resultIntent);

    }

    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
        super.onAbilityResult(requestCode, resultCode, resultData);

    }
}
